package com.yaxin.outsourcing.mapper;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yaxin.common.annotation.DataColumn;
import com.yaxin.common.annotation.DataPermission;
import com.yaxin.common.core.domain.entity.SysUser;
import com.yaxin.outsourcing.domain.po.AccessApplication;
import com.yaxin.outsourcing.domain.vo.AccessApplicationVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
//import org.mapstruct.Mapper;

import java.time.LocalDate;

/**
 * <p>
 * 临时通行证申请单表 Mapper 接口
 * </p>
 *
 * @author zcc
 * @since 2025-07-29
 */
@Mapper
public interface AccessApplicationMapper extends BaseMapper<AccessApplication> {

    @Select("SELECT MAX(CAST(SUBSTRING(app_no, 9) AS UNSIGNED)) " +
            "FROM access_application " +
            "WHERE app_no LIKE CONCAT(#{date}, '%')")
    Integer selectMaxSeqByDate(@Param("date") LocalDate date);

    @Select("SELECT app_uid FROM access_application WHERE app_unit = #{appUnit} LIMIT 1")
    Long selectAppUidByAppUnit(@Param("appUnit") String appUnit);

    @Select("SELECT out_uid FROM access_application WHERE out_unit = #{outUnit} LIMIT 1")
    Long selectOutUidByOutUnit(@Param("outUnit") String outUnit);

    @DataPermission({
        @DataColumn(key = "deptName", value = "d.dept_id"),
        @DataColumn(key = "userName", value = "u.user_id")
    })
    Page<AccessApplication>selectPageAccessApplicationList(@Param("page") Page<AccessApplication> page, @Param(Constants.WRAPPER) Wrapper<AccessApplication> queryWrapper);
}
